﻿//agregar fila control
function AddRow(propertyName
                , tableName
                , fields
                , newRow
                , fieldsHidden
                , maxIndex
                , hasDeleteLink) 
{

    //alert($("#" + maxIndex).val() + " tabla " + $("#" + tableName));
        var table = $("#" + tableName);
        var index = $("#" + maxIndex).val();

        //Incrementar el index en 1
        
        $("#" + maxIndex).val(Number($("#" + maxIndex).val()) + 1);
        
        //Cargar los hidden
        var newTableRow = "<tr id=" + index.toString() + "><td>";

        newTableRow += "<input type='hidden' autocomplete='off' name='" + propertyName + ".index'" +
                                                " value='" + index.toString() + "'/>"
        //alert(fields);
        // alert(index);
        for (x = 0; x < fields.length; x++ )
        {
            newTableRow += "\n <input type='hidden' name='" + propertyName + "[" + index.toString() + "]." + fields[x] + "'" +
                                                    " id='" + propertyName + "[" + index.toString() + "]." + fields[x] + "'" +
                                                    " value='" + newRow[x] + "' />";                                                                                                                         
        }

        if (hasDeleteLink == true)
            newTableRow += "<a href='javascript:DeleteRow(\"" + tableName + "\", " + index.toString() + ")'>Eliminar</a>";

        //Cargar los datos que se ven en la tabla     
        
        for (x = 0; x < newRow.length; x = x + 1) {
        
            //pero no visualizo los ocultos, ejemplos IDs
            if (fieldsHidden[x] != 0) {
                newTableRow += "</td><td>";
                newTableRow += newRow[x];
            }
        }
       

        newTableRow += "</td></tr>";
        //alert(newTableRow);
        table.append(newTableRow);
}

    function AddRowChange(propertyName
                        , tableName                        
                        , fields
                        , newRow
                        , fieldsHidden
                        , fieldToValDuplicate
                        , messageToValDuplicate
                        , maxIndex
                        , hasDeleteLink) {

        var index = $("#" + maxIndex).val();
        //Valido duplicidad si el campo no es -1
        if (fieldToValDuplicate != -1) {
            for (x = 0; x < index; x++) {

                var idToFind = propertyName + "\\[" + x + "\\]\\." + fields[fieldToValDuplicate];
                //alert("idToFind: " + idToFind + " elemento: " + $("#" + idToFind).val())
                if ($("#" + idToFind).val() == newRow[fieldToValDuplicate]) {
                    alert(messageToValDuplicate);
                    return;
                }
            }
        }        
                       
        AddRow(propertyName
             , tableName
             , fields
             , newRow
             , fieldsHidden
             , maxIndex
             , hasDeleteLink) 

        $("#hasChanges").val("true");

    }

    function DeleteRow(tableName, index) {

        $("#" + tableName + " tr:[id=" + index + "]").remove();
        $("#hasChanges").val("true"); 
    }